//
//  DDView_Alert.m
//  GSDacheDriver
//
//  Created by guoshikeji_a on 2017/3/1.
//  Copyright © 2017年 tanyin. All rights reserved.
//

#import "DDView_Alert.h"

@interface DDView_Alert () {
    UILabel *_label;
    
    //
    NSString *_title;
}

@end

@implementation DDView_Alert

- (instancetype)initWithTitle:(NSString *)title {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        _title = title;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    _label = [UILabel new];
    _label.numberOfLines = 0;
    _label.textAlignment = NSTextAlignmentCenter;
    _label.font = SYSTEM_FONT(15);
    _label.textColor = [UIColor whiteColor];
//    _label.backgroundColor = [UIColor yellowColor];
    _label.text = _title;
    [self addSubview:_label];
    //
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(8.0f);
        make.left.mas_equalTo(self).offset(8.0f);
        make.right.mas_equalTo(self).offset(-8.0f);
        make.bottom.mas_equalTo(self).offset(-8.0f);
    }];
}

- (void)showMid {
    if (self.superview && _title) {
        CGSize size = [DDView_Alert sizeOfString:_title attributes:[DDView_Alert attributes]];
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.superview);
            make.centerY.equalTo(self.superview);
            make.width.mas_equalTo(size.width+16.0f);
            make.height.mas_equalTo(size.height+16.0f);
        }];
        self.transform = CGAffineTransformMakeScale(0.5, 0.5);
        [UIView animateWithDuration:0.2 animations:^{
            self.transform = CGAffineTransformMakeScale(1, 1);
        } completion:^(BOOL finished) {
            [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2.0f];
        }];
    }
}
- (void)showBottom {
    if (self.superview && _title) {
        CGSize size = [DDView_Alert sizeOfString:_title attributes:[DDView_Alert attributes]];
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.superview);
            make.bottom.mas_equalTo(self.superview.mas_bottom).offset(-50.0f);
            make.width.mas_equalTo(size.width+16.0f);
            make.height.mas_equalTo(size.height+16.0f);
        }];
        [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2.0f];
    }
}

+ (CGSize)sizeOfString:(NSString *)string attributes:(NSDictionary *)attributes {
    if (string && ![string isEqualToString:@""]) {
        CGSize size = [string boundingRectWithSize:CGSizeMake(200.0f, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
        size.width += 1;
        size.height += 1;
        return size;
    }
    return CGSizeZero;
}
+ (NSDictionary *)attributes {
    return @{NSFontAttributeName:SYSTEM_FONT(15)};
}

@end
